package com.huajin.iot.system.controller;

import com.huajin.iot.common.annotation.Log;
import com.huajin.iot.common.config.Constant;
import com.huajin.iot.common.controller.BaseController;
import com.huajin.iot.common.utils.R;
import com.huajin.iot.system.domain.RoleDO;
import com.huajin.iot.system.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = {"角色管理接口"})
@RequestMapping("/sys/role")
@RestController
public class RoleController extends BaseController {
    @Autowired
    RoleService roleService;


    @ApiOperation("角色列表")
    @RequiresPermissions("sys:role:role")
    @GetMapping("/list")
    @ResponseBody()
    R list() {
        List<RoleDO> roles = roleService.list();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list", roles);
        return R.ok(resultMap);
    }


    @ApiOperation("更新列表")
    @Log("更新角色")
    @RequiresPermissions("sys:role:edit")
    @PostMapping("/update")
    @ResponseBody()
    R update(RoleDO role) {
        if (roleService.update(role) > 0) {
            return R.ok();
        } else {
            return R.error(1, "保存失败");
        }
    }
    @ApiOperation("删除列表")
    @Log("删除角色")
    @RequiresPermissions("sys:role:remove")
    @PostMapping("/remove")
    @ResponseBody()
    R save(Long id) {
        if (roleService.remove(id) > 0) {
            return R.ok();
        } else {
            return R.error(1, "删除失败");
        }
    }
    @ApiOperation("批量删除列表")
    @RequiresPermissions("sys:role:batchRemove")
    @Log("批量删除角色")
    @PostMapping("/batchRemove")
    @ResponseBody
    R batchRemove(@RequestParam("ids[]") Long[] ids) {
        int r = roleService.batchremove(ids);
        if (r == ids.length) {
            return R.ok();
        }
        return R.error();
    }
}
